using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Book.Blog.Core.DTOs;
using Book.Blog.Core.Interfaces;
using System.Threading.Tasks;

namespace Book.Blog.API.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class SearchController : ControllerBase
    {
        private readonly ISearchService _searchService;

        public SearchController(ISearchService searchService)
        {
            _searchService = searchService;
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="request">搜索请求参数</param>
        /// <returns>搜索结果</returns>
        [HttpGet]
        public async Task<ActionResult<SearchResultDTO>> Search([FromQuery] SearchRequestDTO request)
        {
            var result = await _searchService.SearchAsync(request);
            return Ok(result);
        }

        /// <summary>
        /// 重建索引（需管理员）
        /// </summary>
        /// <returns>操作结果</returns>
        [HttpPost("reindex")]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> ReindexAll()
        {
            await _searchService.ReindexAllAsync();
            return Ok();
        }
    }
} 